package ip.ejb.session.facade;
/***********************************************************************
 * Module:  LanguageFacadeBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class LanguageFacadeBean
 ***********************************************************************/

import ip.dto.LanguageDTO;
import ip.ejb.entity.LanguageLocal;
import ip.ejb.entity.LanguageLocalHome;
import ip.ejb.entity.LanguagePK;
import ip.exceptions.CanNotCreateNewLanguageException;
import ip.exceptions.NoSuchLanguageException;
import ip.util.EJBHomeFactory;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
/**
 * @ejb.bean
 *   name="LanguageFacade"
 *   type="Stateless"
 *   jndi-name="ip/ejb/facade/LanguageFacade"
 *   local-jndi-name="ip/ejb/facade/LanguageFacadeLocal"
 * @ejb.transaction type="Supports"
 */
public class LanguageFacadeBean implements SessionBean
{

	private static final long serialVersionUID = 1L;
	private SessionContext ctx;
	private LanguageLocalHome languageLocalHome = null;
	private LanguageLocal languageLocal = null;	
	private LanguagePK languagePK = null;
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * @throws CanNotCreateNewLanguageException 
	 * @ejb.interface-method view-type="local"
	 */
	public void createLanguage(LanguageDTO languageDTO) throws CanNotCreateNewLanguageException{
		try {
			languageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.LanguageLocalHome.class);
			languageLocalHome.create(languageDTO.getName()); 

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (CreateException e) {			
			e.printStackTrace();
			throw new CanNotCreateNewLanguageException(languageDTO);
		}

	}

	/**
	 * @throws NoSuchLanguageException 
	 * @ejb.interface-method view-type="local"
	 */
	public void updateLanguage(LanguageDTO languageDTO) throws NoSuchLanguageException{
		try {
			languagePK = new LanguagePK();
			languagePK.setLanguageId(languageDTO.getLanguageId());		
			languageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.LanguageLocalHome.class);
			languageLocal = languageLocalHome.findByPrimaryKey(languagePK);

			languageLocal.setName(languageDTO.getName());

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchLanguageException(languageDTO);
		}
	}

	/**
	 * @throws NoSuchLanguageException 
	 * @ejb.interface-method view-type="local"
	 */
	public LanguageDTO getLanguage(LanguageDTO languageDTO) throws NoSuchLanguageException{
		try {
			//there is possibility that there is no needed for this method
//			LanguageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/LanguageLocal");
			languageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.LanguageLocalHome.class);
			languagePK = new LanguagePK();
			languagePK.setLanguageId(languageDTO.getLanguageId());
			languageLocal = languageLocalHome.findByPrimaryKey(languagePK);
			languageDTO = createLanguageDTO(languageLocal);

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchLanguageException(languageDTO);
		}
		return languageDTO;

	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public Collection getAllLanguages(){
		Collection languagesDTO = null;
		try {
//			languageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/LanguageLocal");
			languageLocalHome = (LanguageLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.LanguageLocalHome.class);
			Collection languages = languageLocalHome.findAll();
			logger.debug("getAllCountries() - languages: "+languages.size());
			if(languages.size()>0){
				languagesDTO = new ArrayList();
				Iterator iter = languages.iterator();

				while (iter.hasNext()) {
					languageLocal = (LanguageLocal) iter.next();
					languagesDTO.add(createLanguageDTO(languageLocal));
				}
			}

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();			
		}
		return languagesDTO;
	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public LanguageDTO createLanguageDTO(LanguageLocal languageLocal) {			
		LanguageDTO languageDTO = null;
		if(languageLocal != null){
			languageDTO = new LanguageDTO();
			languageDTO.setLanguageId(languageLocal.getLanguageId());
			languageDTO.setName(languageLocal.getName());
		}
		return languageDTO;
	}

	/**
	 * @ejb.create-method
	 */
	public void ejbCreate(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}
	public void ejbRemove(){}
	public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
		this.ctx = ctx;
	}

}